﻿using System;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using SysComm;
using SysOperationLayer.DBHandle;
using Resources;
using IPMS.Common;

public partial class OptClientsUpdate : Page, IRequiresSessionState
{
    protected override void InitializeCulture()
    {
        string strCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();

        try
        {
            strCulture = Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(strCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(strCulture);
        base.InitializeCulture();
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorOrNormalUserType())
        {
            Response.Redirect("ErrorPage/NotAllowed.aspx");
        }
        else if (!IsPostBack)
        {
            ((Label)Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.Settings + " | " + ISitemap.MaintenanceFiles + " | " + ISitemap.ClientsUpdate.ToString();

            ClientsDB objDB = new ClientsDB();
            string client = Request["client"].ToString();
            DataRow dr = objDB.GetItem(client);

            if (dr != null)
            {
                txtClient.Text = objDB.GetValue(dr, "Client");
                txtName.Text = objDB.GetValue(dr, "Name");
                txtChiName.Text = objDB.GetValue(dr, "ChineseName");
                txtAddress.Text = objDB.GetValue(dr, "Address");
                txtWebSite.Text = objDB.GetValue(dr, "WebSite");
                txtJobTitle.Text = objDB.GetValue(dr, "JobTitle");
                txtContactPerson.Text = objDB.GetValue(dr, "ContactPerson");
                txtPhone.Text = objDB.GetValue(dr, "Phone");
                txtFax.Text = objDB.GetValue(dr, "Fax");
                txtEmailAddress.Text = objDB.GetValue(dr, "EmailAddress");
                txtContactPerson2.Text = objDB.GetValue(dr, "ContactPerson2");
                txtPhone2.Text = objDB.GetValue(dr, "Phone2");
                txtFax2.Text = objDB.GetValue(dr, "Fax2");
                txtEmailAddress2.Text = objDB.GetValue(dr, "EmailAddress2");
            }

            objDB.CloseDB();
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("OptClients.aspx");
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        ClientsDB objDB = new ClientsDB();

        if (objDB.Update(txtClient.Text.Trim(),
                            txtName.Text.Trim(),
                            txtChiName.Text.Trim(),
                            txtAddress.Text.Trim(),
                            txtWebSite.Text.Trim(),
                            txtJobTitle.Text.Trim(),
                            txtContactPerson.Text.Trim(),
                            txtPhone.Text.Trim(),
                            txtFax.Text.Trim(),
                            txtEmailAddress.Text.Trim(),
                            txtContactPerson2.Text.Trim(),
                            txtPhone.Text.Trim(),
                            txtFax2.Text.Trim(),
                            txtEmailAddress2.Text.Trim(),
                            Page.User.Identity.Name.ToString(),
                            HttpContext.Current.Request.UserHostAddress) == 0)
        {
            new LogDB(objDB.GetDB()).writeSucLog(SysUtil.m_functiong_client, "Update Client Master Success! Client Code: " + txtClient.Text.Trim());
            Jscript.Alert("Update Client Master Success!", Page);
        }
        else
        {
            new LogDB(objDB.GetDB()).writeFaileLog(SysUtil.m_functiong_client, "Update Client Master Failure! Client Code: " + txtClient.Text.Trim());
            Jscript.Alert("Update Client Master Failure!", Page);
            lblError.Text = objDB.ErrorMessage;
        }

        objDB.CloseDB();
    }

    protected void clearText()
    {
        txtClient.Text = string.Empty;
        txtName.Text = string.Empty;
        txtChiName.Text = string.Empty;
        txtContactPerson.Text = string.Empty;
        txtAddress.Text = string.Empty;
        txtJobTitle.Text = string.Empty;
        txtPhone.Text = string.Empty;
        txtFax.Text = string.Empty;
        txtPhone2.Text = string.Empty;
        txtContactPerson2.Text = string.Empty;
        txtFax2.Text = string.Empty;
        txtWebSite.Text = string.Empty;
        txtEmailAddress.Text = string.Empty;
        txtEmailAddress2.Text = string.Empty;
        lblError.Text = "";
    }
}